package com.ts.api.http.org.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.ts.api.http.org.entity.apply.OrgApplyFeiXin;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.core.utils.ApiFieldConverter;
import com.ts.api.module.api.convert.org.handler.ApiOrgPhoneMd5ConvertHandler;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5Apply;
import com.ts.api.module.api.convert.org.model.RespOrgPhoneMd5DataExchange;

import java.util.Map;

/**
 *  武汉飞信单选
 *
 *  http://8.148.7.4:8888/index/Article/index.html?cate=15
 *
 *  @author WJB
 */
@ApiPlan(productId = "37", type = ApiPlanConvertType.ORG_PHONE_MD5)
public class ApiOrgPhoneMd5FeiXin implements ApiOrgPhoneMd5ConvertHandler {

    private static final Integer CODE = 21;
    private static final String CHANNEL_KEY = "SDFCNDS2FNSIJVN4";

    @Override
    public Object dataExchange(ApiDataExchangeParameter apiDataExchangeParameter) {
        Map<Object, Object> map = MapUtils.newHashMap(newMap -> {
            newMap.put("channel_code", CODE);
            newMap.put("channel_secret_key", CHANNEL_KEY);
            newMap.put("md5_phone", apiDataExchangeParameter.getPhoneMd5());
        });
        return JsonUtils.toJson(map);
    }

    @Override
    public RespOrgPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }

        boolean code = Eq.object(jsonObject.getInteger("code"), 0);
        if (!code) {
            return RespOrgPhoneMd5DataExchange.ofFail();
        }
        return RespOrgPhoneMd5DataExchange.ofSuc();
    }

    @Override
    public Object apply(ApiApplyParameter apiApplyParameter) {
        OrgApplyFeiXin data = new OrgApplyFeiXin();
        data.setChannel_code(CODE);
        data.setName(apiApplyParameter.getUserName());
        data.setWork_unit(apiApplyParameter.getCity());
        AES ecb = AES.createEcb(CHANNEL_KEY.getBytes());
        if (ValidParam.isNotEmpty(apiApplyParameter.getPhone())) {
            data.setPhone(ecb.encryptToBase64Str(apiApplyParameter.getPhone()));
        }
        Integer sex = ChooseEq.create()
                .when(apiApplyParameter.getSex(), "0", 1)
                .when(apiApplyParameter.getSex(), "1", 2)
                .end();
        data.setSex(sex);

        //  拓展判断信息
        ApiFieldConverter apiFieldConverter = ApiFieldConverter.create(apiApplyParameter);
        //  年龄
        data.setAge(apiFieldConverter.getAgeInt());
        //  贷款额度
        if (ValidParam.isNotEmpty(apiApplyParameter.getLoanAmount())) {
            Integer money = ChooseEq.create()
                    .when(apiApplyParameter.getLoanAmount(), "1", 3)
                    .when(apiApplyParameter.getLoanAmount(), "2", 5)
                    .when(apiApplyParameter.getLoanAmount(), "3", 10)
                    .when(apiApplyParameter.getLoanAmount(), "4", 20)
                    .end();
            data.setQuota(money);
        }
        //  房
        if (ValidParam.isNotEmpty(apiApplyParameter.getRealEstateInfo())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getRealEstateInfo(), "1", 0)
                    .end(1);
            data.setHouse(text);
        }
        //  车
        data.setCar(apiFieldConverter.hasCarInfoToNumber());
        //  社保
        data.setSocial_security(apiFieldConverter.hasSocialSecurityToNumber());
        //  信用卡
        data.setCredit_card(apiFieldConverter.hasCreditLimitToNumber());
        //  营业执照
        data.setBusiness_license(apiFieldConverter.hasBizLicenseYearToNumber());
        //  公积金
        data.setAccumulation_fund(apiFieldConverter.hasProvidentFundToNumber());
        //  保单
        data.setInsurance_policy(apiFieldConverter.hasInsurancePolicyToNumber());
        //  代发
        if (ValidParam.isNotEmpty(apiApplyParameter.getSalaryPayMethod())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getSalaryPayMethod(), "1", 1)
                    .end(0);
            data.setHava_issued(text);
        }
        //  身份证
        data.setId_card(apiApplyParameter.getIdcard());
        //  职业
        if (ValidParam.isNotEmpty(apiApplyParameter.getCareer())) {
            Integer text = ChooseEq.create()
                    .when(apiApplyParameter.getCareer(), "1", 1)
                    .when(apiApplyParameter.getCareer(), "2", 0)
                    .when(apiApplyParameter.getCareer(), "3", 2)
                    .when(apiApplyParameter.getCareer(), "4", 1)
                    .when(apiApplyParameter.getCareer(), "5", 0)
                    .end();
            data.setProfessional_identity(text);
        }

        String json = JsonUtils.toJson(data);
        return json;
    }

    @Override
    public RespOrgPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 0)) {
            return RespOrgPhoneMd5Apply.ofFail();
        }
        return RespOrgPhoneMd5Apply.ofSuc();
    }
}
